//
//  ONSettingViewController.m
//  Volatix
//
//  Created by Familly on 6/27/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONSettingViewController.h"
#import "ONSettingScreenViewCell.h"
#import "ONTabbarViewController.h"


//// detail
#import "ONInforViewController.h"
//#import "ONSupportViewController.h"
//#import "ONCopyRightViewController.h"
//#import "ONPolicyViewController.h"
//#import "ONLicenseViewController.h"
#import "ONRatingAppsViewController.h"
@interface ONSettingViewController ()

@end

@implementation ONSettingViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.loginButton.layer.cornerRadius = 5;
    
    // hardcode
    self.dataArray = [[NSMutableArray alloc]initWithObjects:@"Support",@"Copyright",@"Policy",@"License",@"Rating Apps", nil];
    [self.tableView reloadData];
}

-(void)settingLayout
{
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    /// change left Button
    ONTabbarViewController *_tabbar =(ONTabbarViewController*)self.tabBarController;
    [_tabbar.ButtonLeft setHidden:true];
    [_tabbar.buttonSetting setHidden:true];
    _tabbar.title = @"Setting";
    
    
    if ([self userIsLogin]) {
        [self.loginButton setTitle:@"LOGOUT" forState:UIControlStateNormal];
        [self.loginButton setTitle:@"LOGOUT" forState:UIControlStateHighlighted];
    } else {
        [self.loginButton setTitle:@"SIGN IN" forState:UIControlStateNormal];
        [self.loginButton setTitle:@"SIGN IN" forState:UIControlStateHighlighted];
    }
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    ONTabbarViewController *_tabbar =(ONTabbarViewController*)self.tabBarController;
    [_tabbar.ButtonLeft setHidden:false];
    [_tabbar.buttonSetting setHidden:false];
     _tabbar.title = @"Volatix Power Market";
   
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSInteger numberOfCell = [self.dataArray count];
    return numberOfCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSString *identifierString = @"ONSettingScreenViewCell";
        ONSettingScreenViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierString];
        if(cell == nil){
            cell = [[ONSettingScreenViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierString];
        }
    
    NSString *str =([self.dataArray count]<indexPath.row)?@"":[self.dataArray objectAtIndex:indexPath.row];
    [cell loadCell:str];
    if (indexPath.row == [self.dataArray count]-1) {
        cell.lineView.hidden = NO;
    } else {
        cell.lineView.hidden = YES;
    }
    
    return cell;
 
}

#pragma mark - UITableViewDelegate Methods
-(NSString *)getLinkByNumber:(int)value
{
    NSString *str=@"";
    switch (value) {
        case 0:
        {///
            str =[NSString stringWithFormat:@"%@Templates/Support",URL_SERVICE];
        }
            break;
        case 1:
        {
             str =[NSString stringWithFormat:@"%@Templates/Copyright",URL_SERVICE];
        }
            break;
        case 2:
        {
            str =[NSString stringWithFormat:@"%@Templates/Policy",URL_SERVICE];
        }
            break;
        case 3:
        {
             str =[NSString stringWithFormat:@"%@Templates/License",URL_SERVICE];
        }
            break;
        default:
            break;
    }
    
    return str;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    int value =indexPath.row;
    switch (value) {
            case 0:
            case 1:
            case 2:
            case 3:
        {
            ONInforViewController *vc=(ONInforViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ONInforViewController"];
            vc.link =[self getLinkByNumber:value];
            vc.Type=value;
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
        case 4:
            ///Rating Apps
        {
            ONRatingAppsViewController * vc =(ONRatingAppsViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ONRatingAppsViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
       /* case 0:
            ///Support
        {
            ONSupportViewController * vc =(ONSupportViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ONSupportViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 1:
            ///Copyright
        {
            ONCopyRightViewController * vc =(ONCopyRightViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ONCopyRightViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            break;
        case 2:
            ///Policy
        {
            ONPolicyViewController * vc =(ONPolicyViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ONPolicyViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
            ///License
        {
            ONLicenseViewController * vc =(ONLicenseViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ONLicenseViewController"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;*/
        
            
        default:
            break;
    }
    
    
    
    // Deselect the row
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Webservice Methods

// when pull down refresh, this method will be called
- (void)requestGetData {
    // hardcode
    [self performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:1];
}


#pragma mark - Action Methods

- (IBAction)loginButtonPressed:(id)sender {
    if ([self userIsLogin]) {

        [[WebserviceManager sharedInstance] operationWithType:ENUM_API_REQUEST_LOGOUT andPostMethodKind:enumAPIRequestMethod_POST andParams:nil andString:nil inView:nil completeBlock:^(id responseObject) {
             NSLog(@"Log Out Ok");
            [self logoutUser];
            [self.loginButton setTitle:@"SIGN IN" forState:UIControlStateNormal];
            [self.loginButton setTitle:@"SIGN IN" forState:UIControlStateHighlighted];
            
        } failureBlock:^(NSError *error) {
            NSLog(@"Log Out Error");
        }];
        
        
    } else {
        [self showLoginScreen];
    }
}

@end
